package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.PlayerDao;
import com.zwxbc.funplay.entity.Player;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PlayerDaoImpl implements PlayerDao {
	private static final String INSERT_PLAYER = "INSERT INTO player (player_name, player_level, player_username, player_password, player_contact, player_image, player_email) VALUES (?, ?, ?, ?, ?, ?, ?)";
	private static final String SELECT_PLAYER_BY_ID = "SELECT * FROM player WHERE player_id = ?";
	private static final String SELECT_PLAYER_BY_USER_NAME = "SELECT * FROM player WHERE player_username = ?";
	private static final String SELECT_ALL_PLAYERS = "SELECT * FROM player";
	private static final String UPDATE_PLAYER = "UPDATE player SET player_name = ?, player_level = ?, player_username = ?, player_password = ?, player_contact = ?, player_image = ?, player_email = ? WHERE player_id = ?";
	private static final String DELETE_PLAYER = "DELETE FROM player WHERE player_id = ?";
	private static final String LOGIN_PLAYER = "SELECT * FROM player WHERE (player_username = ? or player_contact = ? or player_email = ?) and player_password=?";
	private static final String PLAYER_EXISTS = "SELECT COUNT(*) FROM player WHERE player_username = ? OR player_contact = ? OR player_email = ?";
	private static final String PLAYER_UPDATE = "UPDATE player SET player_name = ?, player_username = ?, player_contact = ?, player_image = ?, player_email = ? WHERE player_username = ?";
	@Override
	public int addPlayer(Player player) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_PLAYER)) {
			preparedStatement.setString(1, player.getPlayerName());
			preparedStatement.setLong(2, player.getPlayerLevel());
			preparedStatement.setString(3, player.getPlayerUsername());
			preparedStatement.setString(4, player.getPlayerPassword());
			preparedStatement.setString(5, player.getPlayerContact());
			preparedStatement.setString(6, player.getPlayerImage());
			preparedStatement.setString(7, player.getPlayerEmail());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public Player playerLogin(Player player) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(LOGIN_PLAYER)) {
			preparedStatement.setString(1, player.getPlayerUsername());
			preparedStatement.setString(2, player.getPlayerContact());
			preparedStatement.setString(3, player.getPlayerEmail());
			preparedStatement.setString(4, player.getPlayerPassword());
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createPlayerFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}


	@Override
	public Player getPlayerByUsername(String playerUsername) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_PLAYER_BY_USER_NAME)) {
			preparedStatement.setString(1, playerUsername);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createPlayerFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}
	
	@Override
	public Player getPlayerById(Long playerId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_PLAYER_BY_ID)) {
			preparedStatement.setLong(1, playerId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createPlayerFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Player> getAllPlayers() {
		List<Player> players = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_PLAYERS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				players.add(createPlayerFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return players;
	}

	@Override
	public int updatePlayer(Player player) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_PLAYER)) {
			preparedStatement.setString(1, player.getPlayerName());
			preparedStatement.setLong(2, player.getPlayerLevel());
			preparedStatement.setString(3, player.getPlayerUsername());
			preparedStatement.setString(4, player.getPlayerPassword());
			preparedStatement.setString(5, player.getPlayerContact());
			preparedStatement.setString(6, player.getPlayerImage());
			preparedStatement.setString(7, player.getPlayerEmail());
			preparedStatement.setLong(8, player.getPlayerId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int playerUpdate(Player player) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(PLAYER_UPDATE)) {
			preparedStatement.setString(1, player.getPlayerName());
			preparedStatement.setString(2, player.getPlayerUsername());
			preparedStatement.setString(3, player.getPlayerContact());
			preparedStatement.setString(4, player.getPlayerImage());
			preparedStatement.setString(5, player.getPlayerEmail());
			preparedStatement.setString(6, player.getPlayerUsername());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deletePlayer(Long playerId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_PLAYER)) {
			preparedStatement.setLong(1, playerId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public boolean isPlayerExists(String username, String contact, String email) {
		int count = 0;
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(PLAYER_EXISTS)) {
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, contact);
			preparedStatement.setString(3, email);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					count = resultSet.getInt(1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count > 0;
	}

	// Helper 方法，从 ResultSet 中创建 Player 对象
	private Player createPlayerFromResultSet(ResultSet resultSet) throws SQLException {
		Player player = new Player();
		player.setPlayerId(resultSet.getLong("player_id"));
		player.setPlayerEmail(resultSet.getString("player_email"));
		player.setPlayerName(resultSet.getString("player_name"));
		player.setPlayerLevel(resultSet.getLong("player_level"));
		player.setPlayerUsername(resultSet.getString("player_username"));
		player.setPlayerPassword(resultSet.getString("player_password"));
		player.setPlayerContact(resultSet.getString("player_contact"));
		player.setPlayerImage(resultSet.getString("player_image"));
		return player;
	}
}
